package github.flandre.examplemod.core.init.event;

import github.flandre.examplemod.common.entity.ExampleEntity;
import github.flandre.examplemod.common.mycapability.ISpeedUpCapability;
import github.flandre.examplemod.common.mycapability.SpeedUpCapabilityProvider;
import github.flandre.examplemod.core.registry.CommonSetupEventHandler;
import github.flandre.examplemod.examplemod;
import github.flandre.examplemod.network.NetworkRegistryHandler;
import github.flandre.examplemod.network.Packet.Power;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.FrameTimer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.event.AttachCapabilitiesEvent;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.network.PacketDistributor;


@Mod.EventBusSubscriber
public class ExamplePowerEvent {
    //给玩家添加能力
    @SubscribeEvent
    public static void onAttackCapabilityEvent(AttachCapabilitiesEvent<Entity> event)
    {
        Entity object = event.getObject();
        if(object instanceof PlayerEntity)
        {
            event.addCapability(new ResourceLocation(examplemod.MODID, "speedup"), new SpeedUpCapabilityProvider());
        }
    }

    //玩家加入时将数值打印到聊天框
    @SubscribeEvent
    public static void onPlayerJoin(EntityJoinWorldEvent event)
    {
        Entity entity = event.getEntity();
        //在服务器上 实体是玩家
        if(!event.getWorld().isRemote && entity instanceof PlayerEntity)
        {
            PlayerEntity playerEntity = (PlayerEntity) entity;
            //获取到被包装过的能力
            LazyOptional<ISpeedUpCapability> capability = playerEntity.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
            capability.ifPresent((c)->{
                float Power = c.getLevel();
                //向聊天栏发送
                playerEntity.sendMessage(
                        new StringTextComponent("your example power is:"+Power),
                        playerEntity.getUniqueID()
                );
                //同步数据 往服务端发包是sendToServer
                NetworkRegistryHandler.CHANNEL.send(
                        PacketDistributor.PLAYER.with(
                                ()->(ServerPlayerEntity) playerEntity),//为了区分不同的玩家
                        new Power(c.getLevel())
                );
            });

        }
    }

    //处理死亡刷新
    @SubscribeEvent
    public static void onPlayerClone(PlayerEvent.Clone event)
    {
        //玩家死亡前的对象
        PlayerEntity original = event.getOriginal();
        //玩家死亡后的对象
        PlayerEntity player = event.getPlayer();

        LazyOptional<ISpeedUpCapability> originalcapability = original.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
        LazyOptional<ISpeedUpCapability> capability = player.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);

        if(originalcapability.isPresent() && capability.isPresent())
        {
            originalcapability.ifPresent((a)->{
                capability.ifPresent((b)->{
                    b.setLevel(a.getLevel());
                    //将玩家死亡前的数值给与到死亡后

                    NetworkRegistryHandler.CHANNEL.send(
                            PacketDistributor.PLAYER.with(
                                    ()->(ServerPlayerEntity) player),//为了区分不同的玩家
                            new Power(b.getLevel())
                    );
                });
            });
        }


        //处理commmands 中玩家死亡数组消失的问题
        if(!original.getEntityWorld().isRemote())
        {
            player.getPersistentData().putIntArray(
                    examplemod.MODID + ".save_pos",
                    original.getPersistentData().getIntArray(examplemod.MODID + ".save_pos")
            );
        }
    }



    //土球王受到伤害增加点数
    @SubscribeEvent
    public static void onLivingDamage(LivingDamageEvent event)
    {
        LivingEntity entityLiving = event.getEntityLiving();
        if(entityLiving instanceof ExampleEntity)
        {
            //生物的血量 造成的伤害
            float amount = Math.min(entityLiving.getHealth(),event.getAmount());
            //获得造成伤害的人
            Entity trueSource = event.getSource().getTrueSource();
            if(trueSource instanceof PlayerEntity)
            {
                PlayerEntity playerEntity = (PlayerEntity) trueSource;
                LazyOptional<ISpeedUpCapability> capability = playerEntity.getCapability(CommonSetupEventHandler.SPEED_UP_CAPABILITY);
                capability.ifPresent((c)->{
                    float examplePower = c.getLevel();
                    c.setLevel(examplePower + amount);


                    //向聊天栏发送
                    playerEntity.sendMessage(
                            new StringTextComponent("your example power is:"+c.getLevel()),
                            playerEntity.getUniqueID()
                    );


                    //同步数据
                    NetworkRegistryHandler.CHANNEL.send(
                            PacketDistributor.PLAYER.with(
                                    ()->(ServerPlayerEntity) playerEntity),//为了区分不同的玩家
                            new Power(c.getLevel())
                    );
                });
            }
        }

    }

}
